<?php defined('SYS_PATH') or die('No direct script access.');
/**
 * Cookie class
 *
 * @author zendzhang<zendzhang@hotmail.com>
 * @version $Id: Cookie.php 9 2013-01-14 03:24:50Z zendzhang@hotmail.com $
 */
class TrueBlood_Cookie extends TrueBlood
{
    /**
     * Configuration
     *
     * @var array
     */
    protected $_config = array
    (
        'prefix'   => '',
        'path'     => '/',
        'domain'   => '',
        'secure'   => false,
        'httponly' => false
    );

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->_config = self::$load->config('cookie') + $this->_config;

        $this->log('debug', "TrueBlood_Cookie Class Initialized");
    }

    /**
     * Sets a signed cookie.
     *
     * @param string $name
     * @param string $value
     * @param int $expire
     * @return bool
     */
    public function set($name, $value, $expire = 0)
    {
        $expire = ($expire == 0) ? 0 : time() + (int) $expire;
        $params = array
        (
            $this->_config['prefix'] . $name,
            $value,
            $expire,
            $this->_config['path'],
            $this->_config['domain'],
            $this->_config['secure']
        );
        if (version_compare(PHP_VERSION, '5.2', '>='))
        {
            $params[] = $this->_config['httponly'];
        }
        return call_user_func_array('setcookie', $params);
    }

    /**
     * Gets the value of a signed cookie.
     *
     * @param string $name
     * @param mixed $default
     * @return mixed
     */
    public function get($name, $default = null)
    {
        if (isset($_COOKIE[$this->_config['prefix'] . $name]))
        {
            return $_COOKIE[$this->_config['prefix'] . $name];
        }
        return $default;
    }

    /**
     * Deletes a cookie by making the value empty and expiring it.
     *
     * @param string $name
     * @return void
     */
    public function del($name)
    {
        return $this->delete($name);
    }

    /**
     * Deletes a cookie by making the value empty and expiring it.
     *
     * @param string $name
     * @return void
     */
    public function delete($name)
    {
        return $this->set($name, '', -86400);
    }
}

?>